package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.ActivityDao;
import com.gzy.studentsociety.dao.ActivityDaoImpl;
import com.gzy.studentsociety.dao.SocietyDao;
import com.gzy.studentsociety.dao.SocietyDaoImpl;
import com.gzy.studentsociety.entity.Activity;
import com.gzy.studentsociety.entity.Society;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "ActivityServlet", value = "/activity/*")
public class ActivityServlet extends RouteServlet {

    private SocietyDao societyDao = new SocietyDaoImpl();
    private ActivityDao activityDao = new ActivityDaoImpl();

    @Override
    protected Class getMyClass() {
        return this.getClass();
    }

    public String addPage(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        HttpSession session = req.getSession();
        String number = (String) session.getAttribute("student");
        Society society = null;
        try {
            society = societyDao.queryByStudentNumber(number);
        } catch (SQLException e) {
            handleException(e, req, res);
            return null;
        }
        req.setAttribute("society", society);
        session.setAttribute("society", society);
        return "activity/addActivity";
    }

    public String addActivity(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        String startStr = req.getParameter("start").replaceAll("T", " ");
        String endStr = req.getParameter("end").replaceAll("T", " ");
        String assIdStr = req.getParameter("assId");
        Integer assId = Integer.valueOf(assIdStr);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date start = null;
        Date end = null;
        Integer result = null;

        try {
            start = format.parse(startStr);
            end = format.parse(endStr);
        } catch (ParseException e) {
            e.printStackTrace();
            req.setAttribute("error", "日期格式错误");
            return "activity/addActivity";
        }

        if (!start.after(new Date())) {
            req.setAttribute("error", "活动开始时间必须晚于当前时间");
            return "activity/addActivity";
        }

        Activity activity = new Activity();
        activity.setActivity_societyId(assId);
        activity.setActivity_name(name);
        activity.setActivity_intro(intro);
        activity.setActivity_start_time(start);
        activity.setActivity_end_time(end);

        try {
            result = activityDao.addActivity(activity);
        } catch (SQLException e) {
            handleException(e, req, res);
            return null;
        }

        req.setAttribute("result", result);
        return "activity/addActivity";
    }

    public String activityList(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        HttpSession session = req.getSession();
        String number = (String) session.getAttribute("student");
        Society society = null;
        List<Activity> activities = null;
        try {
            society = societyDao.queryByStudentNumber(number);
            activities = activityDao.queryBySocietyId(society.getSocietyId());
        } catch (SQLException e) {
            handleException(e, req, res);
            return null;
        }
        req.setAttribute("society", society);
        req.setAttribute("activities", activities);
        return "activity/activityList";
    }

    public Object updateActivity(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String idStr = req.getParameter("id");
        String intro = req.getParameter("intro");
        Integer id = Integer.valueOf(idStr);
        Integer result = null;
        try {
            result = activityDao.updateIntro(id, intro);
        } catch (SQLException e) {
            handleException(e, req, res);
            return null;
        }
        return result;
    }

    private void handleException(Exception e, HttpServletRequest req, HttpServletResponse res) {
        e.printStackTrace();
        if (!res.isCommitted()) {
            try {
                res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器内部错误");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
